package com.smartlib.cmnObject.ui.searchView;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smartlib.cmnObject.R;
import com.smartlib.cmnObject.util.DeviceUtil;
import com.smartlib.cmnObject.util.LogUtil;


/**
 * @author：hww
 * @date:2016/5/9
 * @description:<search view>
 */
public class SearchView extends RelativeLayout implements View.OnClickListener
{
    /***
     * 输入框
     */
    private EditText mEtInput;

    private ImageView mImDrawableLeft;
    /***
     * 删除按钮
     */
    private ImageView mImDel;
    /***
     * 输入框的背景
     */
    private int mInputBackgroud;
    /***
     * hint
     */
    private int mInputHintId;
    /***
     * textsize hintTextsize
     */
    private int mEditTextSize, mEditTextHintColor, mEditTextColor;

    private String mInputTypeString = "";

    private int mInputMaxLength = 0;

    private int mInputPaddingLeft;

    private int mInputPaddingTop, mInputPaddingBottom;


    private RelativeLayout mRlSearch;

    private int mDrawableLeftId;

    private SearchData mSearchData;

    private String mImeOptions;

    private EditorActionListener mEditorActionListener;

    private View view;


    public SearchView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.searchView);
        mInputBackgroud = a.getResourceId(R.styleable.searchView_search_background, -1);
        mInputHintId = a.getResourceId(R.styleable.searchView_search_hint, -1);
        mEditTextSize = a.getResourceId(R.styleable.searchView_search_size, -1);
        mEditTextHintColor = a.getResourceId(R.styleable.searchView_search_hintColor, -1);
        mInputTypeString = a.getString(R.styleable.searchView_search_inputType);
        mInputMaxLength = a.getInt(R.styleable.searchView_search_maxLength, -1);
        mEditTextColor = a.getResourceId(R.styleable.searchView_search_textColor, -1);
        mInputPaddingLeft = DeviceUtil.dip2px(context, a.getResourceId(R.styleable.searchView_search_paddingLeft, 0));
        mDrawableLeftId = a.getResourceId(R.styleable.searchView_search_DrawableLeft, -1);
        mImeOptions = a.getString(R.styleable.searchView_search_imeOptions);
        mInputPaddingTop = DeviceUtil.dip2px(context, a.getResourceId(R.styleable.searchView_search_paddingTop, 0));
        mInputPaddingBottom = DeviceUtil.dip2px(context, a.getResourceId(R.styleable.searchView_search_paddingBottom, 0));
        a.recycle();
        initView(context);
    }

    private void initView(Context context)
    {
        view = LayoutInflater.from(context).inflate(R.layout.edittext_has_del, null);
        mRlSearch = (RelativeLayout) view.findViewById(R.id.rl_search);
        mImDrawableLeft = (ImageView) view.findViewById(R.id.im_drawableLeft);
        mEtInput = (EditText) view.findViewById(R.id.et_input);
        mImDel = (ImageView) view.findViewById(R.id.im_del);
        mImDel.setOnClickListener(this);
        mEtInput.setOnFocusChangeListener(new OnFocusChangeListener()
        {
            @Override
            public void onFocusChange(View v, boolean hasFocus)
            {
                if (hasFocus)
                {
                    if (mEtInput.getText().length() > 0)
                    {
                        mImDel.setVisibility(VISIBLE);
                    }
                    else
                    {
                        mImDel.setVisibility(GONE);
                    }
                }
                else
                {
                    mImDel.setVisibility(GONE);
                }
            }
        });
        initData(context);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        addView(view, params);
    }


    private void initData(Context context)
    {
        if (mInputBackgroud > 0)
        {
            mRlSearch.setBackgroundResource(mInputBackgroud);
        }
        if (mInputHintId > 0)
        {
            mEtInput.setHint(mInputHintId);
        }
        if (mEditTextSize > 0)
        {
            mEtInput.setTextSize(DeviceUtil.px2sp(context, context.getResources().getDimension(mEditTextSize)));
        }
        if (mEditTextHintColor > 0)
        {
            mEtInput.setHintTextColor(mEditTextHintColor);
        }
        if (!TextUtils.isEmpty(mInputTypeString))
        {
            if (mInputTypeString.equals("number"))
            {
                mEtInput.setInputType(InputType.TYPE_CLASS_NUMBER);
            }
            else if (mInputTypeString.equals("textPassword"))
            {
                mEtInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
        }

        if (mInputMaxLength > 0)
        {
            mEtInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mInputMaxLength)});
        }

        if (mEditTextColor > 0)
        {
            mEtInput.setTextColor(mEditTextColor);
        }
        view.setPadding(mInputPaddingLeft, mInputPaddingTop, 0, mInputPaddingBottom);

        if (mDrawableLeftId > 0)
        {
            mImDrawableLeft.setBackgroundResource(mDrawableLeftId);
        }


        mEtInput.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {

            }

            @Override
            public void afterTextChanged(Editable s)
            {
                if (!TextUtils.isEmpty(s) && s.length() > 0)
                {
                    mImDel.setVisibility(VISIBLE);
                }
                else
                {
                    mImDel.setVisibility(GONE);
                }
                if (mSearchData != null)
                {
                    mSearchData.searchData(s.toString());
                }
            }
        });

        if (!TextUtils.isEmpty(mImeOptions))
        {
            if (mImeOptions.equals("actionSearch"))
            {
                mEtInput.setSingleLine();
                mEtInput.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
                //有搜索按钮
                mEtInput.setOnEditorActionListener(new TextView.OnEditorActionListener()
                {
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
                    {
                        if (actionId == EditorInfo.IME_ACTION_SEARCH)
                        {
                            if (mEditorActionListener != null)
                            {
                                LogUtil.logI(v.getText().toString());
                                mEditorActionListener.onEditorAction(v.getText().toString());
                            }

                            return true;
                        }
                        return false;
                    }
                });

            }
            else
            {

            }
        }
    }

    @Override
    public void onClick(View v)
    {
        int i = v.getId();
        if (i == R.id.im_del)
        {
            mEtInput.setText("");
        }
        else
        {
        }
    }


    public String getText()
    {
        return mEtInput.getText().toString();
    }

    public void setSearchData(SearchData mSearchData)
    {
        this.mSearchData = mSearchData;
    }

    public void setEditorActionListener(EditorActionListener editorActionListener)
    {
        mEditorActionListener = editorActionListener;
    }
}
